00001 #include "LinearConstraint.h"
00002
00003 osg::DrawArrays* LinearConstraint::makeRoad(void ) const
00004 {
00005 return new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,2*_midline->size());
00006
00007 }
00008
00009 osg::Vec3Array *LinearConstraint::getRoadNormals(const osg::Vec3Array* ) const
00010 {
00011 osg::Vec3Array *nrms=new osg::Vec3Array;
00012 for(unsigned int i=0;i<_midline->size();i++) {
00013 osg::Vec3 valong;
00014 if (i==0) {
00015 valong=(*_midline)[i+1]-(*_midline)[i];
00016 } else if (i==_midline->size()-1) {
00017 valong=(*_midline)[i]-(*_midline)[i-1];
00018 } else {
00019 valong=(*_midline)[i+1]-(*_midline)[i-1];
00020 }
00021 osg::Vec3 vperp=valong^osg::Vec3(0,0,1);
00022 osg::Vec3 nrm=vperp^valong;
00023 nrm.normalize();
00024 nrms->push_back(nrm);
00025 nrms->push_back(nrm);
00026 }
00027 return nrms;
00028 }
00029 osg::Vec3Array *LinearConstraint::getRoadVertices() const
00030 {
00031 osg::Vec3Array *linearEdges=new osg::Vec3Array;
00032 for(unsigned int i=0;i<_midline->size();i++) {
00033 osg::Vec3 valong;
00034 if (i==0) {
00035 valong=(*_midline)[i+1]-(*_midline)[i];
00036 } else if (i==_midline->size()-1) {
00037 valong=(*_midline)[i]-(*_midline)[i-1];
00038 } else {
00039 valong=(*_midline)[i+1]-(*_midline)[i-1];
00040 }
00041 valong.normalize();
00042 osg::Vec3 vperp=valong^osg::Vec3(0,0,1);
00043
00044 linearEdges->push_back((*_midline)[i]-vperp*.5*width);
00045 linearEdges->push_back((*_midline)[i]+vperp*.5*width);
00046 }
00047 return linearEdges;
00048 }
00049
00050 osg::Vec2Array *LinearConstraint::getRoadTexcoords(const osg::Vec3Array *points) {
00051
00052 osg::Vec3Array::const_iterator tritr;
00053 osg::ref_ptr<osg::Vec2Array> tcoords= new osg::Vec2Array ;
00054 for (tritr=points->begin(); tritr!=points->end();tritr++) {
00055 osg::Vec2 tci(-1.,-1.);
00056 int ib=0;
00057 bool ptfound=false;
00058 for (osg::Vec3Array::iterator vit=_edgecoords->begin(); vit!= _edgecoords->end() && !ptfound; vit++) {
00059 if ((*vit)==(*tritr)) {
00060 tci=_tcoords->at(ib);
00061 ptfound=true;
00062 }
00063 ib++;
00064 }
00065 if (!ptfound) {
00066 ib=0;
00067 osg::Vec3 pminus=(_edgecoords->back());
00068 int ibm1=_edgecoords->size()-1;
00069 for (osg::Vec3Array::iterator vit=_edgecoords->begin(); vit!= _edgecoords->end(); vit++) {
00070 osg::Vec3 pplus=(*vit)-(*tritr);
00071 osg::Vec3 dpm=pminus-(*tritr);
00072 pplus.set (pplus.x(),pplus.y(),0);
00073 dpm.set (dpm.x(),dpm.y(),0);
00074 float dprod=pplus*dpm/(pplus.length() * dpm.length());
00075 if (dprod<-0.9999) {
00076 osg::Vec2 tminus=_tcoords->at(ibm1);
00077 osg::Vec2 tplus=_tcoords->at(ib);
00078 float frac=(dpm.length()/(dpm.length()+pplus.length()));
00079 tci=tminus+((tplus-tminus)*frac);
00080 ptfound=true;
00081 }
00082 ibm1=ib;
00083 ib++;
00084 pminus=(*vit);
00085 }
00086 }
00087 tcoords->push_back(tci);
00088 }
00089
00090 for (tritr=points->begin(); tritr!=points->end();tritr++) {
00091 int ib=tritr-points->begin();
00092 osg::Vec2 tci=tcoords->at(ib);
00093 if (tci.x()<-.99 && tci.y()<-.99) {
00094
00095 osg::Vec3Array::const_iterator ptitr;
00096 for (ptitr=points->begin(); ptitr!=points->end();ptitr++) {
00097 }
00098 }
00099 }
00100 return tcoords.release();
00101 }
00102 osg::Vec3Array * LinearConstraint::getNormals(const osg::Vec3Array *points)
00103 {
00104 osg::ref_ptr<osg::Vec3Array> norms=new osg::Vec3Array;
00105 for (osg::DrawElementsUInt::iterator uiitr=prim_tris_->begin(); uiitr!=prim_tris_->end();uiitr+=3) {
00106 osg::Vec3 e1=(*points)[*(uiitr+1)]-(*points)[(*uiitr)];
00107 osg::Vec3 e2=(*points)[*(uiitr+2)]-(*points)[*(uiitr+1)];
00108 osg::Vec3 n=e1^e2;
00109 n.normalize();
00110 norms->push_back(n);
00111 }
00112 return norms.release();
00113 }
00114
00115 osg::Geometry * LinearConstraint::makeGeometry(const osg::Vec3Array *points)
00116 {
00117 osg::ref_ptr<osg::Geometry> gm=new osg::Geometry;
00118 if (_midline->size()>0) {
00119 osg::ref_ptr<osg::Vec3Array> locpts=getPoints(points);
00120 if (texture!="") {
00121 osg::Image* image = osgDB::readImageFile(texture.c_str());
00122 if (image)
00123 {
00124 osg::Texture2D* txt = new osg::Texture2D;
00125 osg::StateSet* stateset = gm->getOrCreateStateSet();
00126 txt->setImage(image);
00127 txt->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
00128 txt->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
00129 stateset->setTextureAttributeAndModes(0,txt,osg::StateAttribute::ON);
00130 osg::Material* material = new osg::Material;
00131 material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
00132 material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
00133 stateset->setAttribute(material,osg::StateAttribute::ON);
00134 stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON );
00135 }
00136 gm->setTexCoordArray(0,getRoadTexcoords(locpts.get()));
00137 }
00138 gm->setVertexArray(locpts.get());
00139 gm->setNormalArray(getNormals(locpts.get()));
00140 gm->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
00141 gm->addPrimitiveSet(getTriangles());
00142 }
00143
00144 return gm.release();
00145
00146 }
00147
00148 void LinearConstraint::setVertices( osg::Vec3Array *lp, const float w)
00149 {
00150 osg::ref_ptr<osg::Vec3Array> edges=new osg::Vec3Array;
00151 _tcoords=new osg::Vec2Array;
00152 _edgecoords=new osg::Vec3Array;
00153 width=w;
00154 _midline=lp;
00155 float ds=0;
00156 for(unsigned int i=0;i<lp->size();i++) {
00157 osg::Vec3 valong;
00158 osg::Vec3 pos[2];
00159
00160 if (i==0) {
00161 valong=(*lp)[i+1]-(*lp)[i];
00162 } else if (i==lp->size()-1) {
00163 valong=(*lp)[i]-(*lp)[i-1];
00164 } else {
00165 valong=(*lp)[i+1]-(*lp)[i-1];
00166 }
00167 valong.normalize();
00168 osg::Vec3 vperp=valong^osg::Vec3(0,0,1);
00169 pos[0]=(*lp)[i]-vperp*.5*width;
00170 pos[1]=(*lp)[i]+vperp*.5*width;
00171 edges->push_back(pos[0]);
00172 _edgecoords->push_back(pos[0]);
00173 _tcoords->push_back(osg::Vec2(0/txyrepAcross,ds/txxrepAlong));
00174 edges->insert(edges->begin() ,pos[1]);
00175 _edgecoords->insert(_edgecoords->begin() ,pos[1]);
00176 _tcoords->insert(_tcoords->begin() ,osg::Vec2(width/(txyrepAcross),ds/txxrepAlong));
00177 if (i<lp->size()-1) ds+=((*lp)[i+1]-(*lp)[i]).length();
00178 }
00179
00180 setVertexArray(edges.get());
00181 addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,edges->size()) );
00182 }
00183